/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web.tmessage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.tmessage.BusinessMessageTemplate;
import com.boxin.ims.modules.wechat.service.tmessage.BusinessMessageTemplateService;

/**
 * 系统消息模板Controller
 * @author 肖乾
 * @version 2014-09-11
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/tmessage/businessMessageTemplate")
public class BusinessMessageTemplateController extends BaseController {

	@Autowired
	private BusinessMessageTemplateService businessMessageTemplateService;
	
	@ModelAttribute
	public BusinessMessageTemplate get(@RequestParam(required=false) Long id) {
		if (id != null){
			return businessMessageTemplateService.get(id);
		}else{
			return new BusinessMessageTemplate();
		}
	}
	
	@RequiresPermissions("wechat:tmessage:businessMessageTemplate:view")
	@RequestMapping(value = {"list", ""})
	public String list(BusinessMessageTemplate businessMessageTemplate, HttpServletRequest request, HttpServletResponse response, Model model, RedirectAttributes redirectAttributes) {
		User user = UserUtils.getUser();
		if(!user.getInterfaceFlag()){
			addMessage(redirectAttributes, "您没有开通高级接口！！");
			return "modules/wechat/tmessage/businessMessageTemplateList";
		}
		if (!user.isAdmin()){
			businessMessageTemplate.setUser(user);
		}
        Page<BusinessMessageTemplate> page = businessMessageTemplateService.find(new Page<BusinessMessageTemplate>(request, response), businessMessageTemplate); 
        model.addAttribute("page", page);
		return "modules/wechat/tmessage/businessMessageTemplateList";
	}

	@RequiresPermissions("wechat:tmessage:businessMessageTemplate:view")
	@RequestMapping(value = "form")
	public String form(BusinessMessageTemplate businessMessageTemplate, Model model) {
	
		User user = UserUtils.getUser();
		if (businessMessageTemplate.getUser() == null || businessMessageTemplate.getUser().getId() <1){
			businessMessageTemplate.setUser(user);
		}
		
		model.addAttribute("businessMessageTemplate", businessMessageTemplate);
		return "modules/wechat/tmessage/businessMessageTemplateForm";
	}

	@RequiresPermissions("wechat:tmessage:businessMessageTemplate:edit")
	@RequestMapping(value = "save")
	public String save(BusinessMessageTemplate businessMessageTemplate, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, businessMessageTemplate)){
			return form(businessMessageTemplate, model);
		}
		User user = UserUtils.getUser();
		if(!user.getInterfaceFlag()){
			addMessage(redirectAttributes, "您没有开通高级接口！！");
			return "redirect:"+Global.ADMIN_PATH+"/wechat/tmessage/businessMessageTemplate/?repage";
		}
		businessMessageTemplateService.save(businessMessageTemplate);
		addMessage(redirectAttributes, "保存系统消息模板'" + businessMessageTemplate.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/tmessage/businessMessageTemplate/?repage";
	}
	
	@RequiresPermissions("wechat:tmessage:businessMessageTemplate:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		businessMessageTemplateService.delete(id);
		addMessage(redirectAttributes, "删除系统消息模板成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/tmessage/businessMessageTemplate/?repage";
	}

}
